package cn.itcast.controller;

import cn.itcast.exception.NameIsNullException;
import cn.itcast.exception.noAccountException;
import cn.itcast.exception.noMoneyException;
import cn.itcast.pojo.Account;
import cn.itcast.service.Uservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("money")
public class ToMoneyController {
    @Autowired
    private Uservice uservice;

    @RequestMapping("toMoney3")
    public String toMoney() {
        return "toMoney2";
    }

    @RequestMapping(value = "toMoney", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toMoney(String outName, String inName, Double money) throws Exception {
        if (outName != null && inName != null && uservice.findByName(outName).getMoney() < money) {
            throw new noMoneyException("传出的账户金钱不足");
        }else if(outName == null || inName == null || "".equals(outName) || "".equals(inName)){
            throw new NameIsNullException("你输入的名字为空,请返回重新输入!!!!");
        } else if(outName != null && inName != null &&uservice.findByName(outName) == null || uservice.findByName(inName) == null){
            throw new noAccountException("没有你输入的账户,请重新输入!!!");
        }else {
            uservice.toMoneyByName(outName, inName, money);
            return "转账成功,请返回查询页面确认!!!";
        }
    }

}
